package com.itfreer.file.path;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 定义对象管理接口
 */
public class FileManage extends BasePath {

	/**
	 * 单一实例
	 */
	public static final FileManage instance = new FileManage();

	private FileManage(){

	}

	/**
	 * 对象是否存在
	 * 
	 * @param bucketName
	 *            存储块名称
	 * @param objectKey
	 *            对象名称主键
	 */
	public boolean doesObjectExist(String bucketName, String objectKey) {
		String path = getBasePath();
		if (path.endsWith("/")) {
			path += bucketName + "/";
		} else {
			path += "/" + bucketName + "/";
		}
		path += objectKey;
		File fileinfo = new File(path);
		return fileinfo.isFile();
	}

	/**
	 * 获取对象名称
	 * 
	 * @param bucketName
	 *            存储块名称
	 * @param prefix
	 *            限定返回的object key必须以prefix作为前缀
	 * @param marker
	 *            设定结果从marker之后按字母排序的第一个开始返回
	 * @param maxKeys
	 *            限定此次返回object的最大数，如果不设定，默认为100，max-keys取值不能大于1000
	 * @return
	 */
	public List<Map<String, Object>> listObjects(String bucketName, String prefix, String marker, Integer maxKeys) {
		String path = getBasePath();
		if (path.endsWith("/")) {
			path += bucketName + "/";
		} else {
			path += "/" + bucketName + "/";
		}

		if (prefix.startsWith("/")) {
			prefix = prefix.substring(1);
		}
		if (!prefix.endsWith("/")) {
			prefix = prefix + "/";
		}
		path += prefix;

		File file = new File(path);// File类型可以是文件也可以是文件夹
		File[] fileList = file.listFiles();// 将该目录下的所有文件放置在一个File类型的数组中
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < fileList.length; i++) {
			if (fileList[i].isFile()) {// 判断是否为文件
				File itemFile = fileList[i];
				if (itemFile.getName().equals(".DS_Store")) {
					continue;
				}
				Map<String, Object> item = new HashMap<String, Object>();
				item.put("key", prefix + itemFile.getName());
				item.put("size", itemFile.getFreeSpace());
				item.put("lastModified", itemFile.lastModified());
				item.put("owner", "");
				mapList.add(item);
			}
		}

		return mapList;
	}

	/**
	 * 删除对象
	 * 
	 * @param bucketName
	 *            存储块名称
	 * @param objectKey
	 *            对象名称主键
	 */
	public void deleteObject(String bucketName, String objectKey) {
		String path = getBasePath();
		if (path.endsWith("/")) {
			path += bucketName + "/";
		} else {
			path += "/" + bucketName + "/";
		}
		path += objectKey;
		File fileinfo = new File(path);
		fileinfo.deleteOnExit();
	}

	/**
	 * 删除多个对象
	 * 
	 * @param bucketName
	 *            存储块名称
	 * @param keys
	 *            对象名称主键
	 * @return
	 */
	public void deleteObjects(String bucketName, List<String> keys) {
		for (String key : keys) {
			deleteObject(bucketName, key);
		}
	}

	/**
	 * 复制对象
	 * 
	 * @param sourceBucketName
	 *            源存储块名称
	 * @param sourceKey
	 *            源对象名称主键
	 * @param destinationBucketName
	 *            目标储块名称
	 * @param destinationKey
	 *            目标对象名称主键
	 * @return
	 */
	public void copyObject(String sourceBucketName, String sourceKey, String destinationBucketName,
			String destinationKey) {
		String oldPath = getBasePath();
		if (oldPath.endsWith("/")) {
			oldPath += sourceBucketName + "/";
		} else {
			oldPath += "/" + sourceBucketName + "/";
		}
		oldPath += sourceKey;

		String newPath = getBasePath();
		if (newPath.endsWith("/")) {
			newPath += destinationBucketName + "/";
		} else {
			newPath += "/" + destinationBucketName + "/";
		}
		newPath += destinationKey;

		try {
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) { // 文件存在时
				FileInputStream inStream = new FileInputStream(oldPath); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				fs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}